/*
 * @Author: wwssaabb
 * @Date: 2021-08-10 17:42:15
 * @LastEditTime: 2021-08-11 15:30:23
 * @FilePath: \react-admin-client\src\redux\reducer.js
 */
/* 
  用来根据旧的state和action来生成新的state的纯函数
*/
import { combineReducers } from 'redux'
import { getUser } from '../utils/storageUtils'

import { 
  SET_HEAD_TITLE,
  SET_USER,
  SET_CATEGORIES,
  RESET_USER
 } from './constant'

/* 
  headerTitle，用来管理标题
*/
const initHeadTitle='首页'
const headTitle=(state=initHeadTitle,action)=>{
  switch(action.type){
    case SET_HEAD_TITLE:
      return action.data
    default:
      return state
  }
}

/* 
  user,用来管理当前登录的用户对象
*/
const initUser=getUser()
const user=(state=initUser,action)=>{
  switch(action.type){
    case SET_USER:
      return action.user
    case RESET_USER:
      return action.user
    default:
      return state
  }
}

/* 
  categories，用来管理分类列表
*/
const initCategories=[]
const categories=(state=initCategories,action)=>{
  switch(action.type){
    case SET_CATEGORIES:
      return action.categories
    default:
      return state
  }
}


export default combineReducers({
  headTitle,
  user,
  categories
})